How to convert arrays and strings in PHP (must read) |
您所在的位置:网站首页 › php 转换为字符串 › How to convert arrays and strings in PHP (must read) |
In PHP, arrays and strings can be converted. Today we will introduce the method of converting arrays and strings. Friends in need can refer to it. 1. Convert array to stringimplodeMethod introduction: function implode ($glue = "", array $pieces) {}Copy after loginMainly has two parameters One is the connector (glue: the meaning of glue), the default is an empty string The other is the array Use $test = array("hello","world","php"); echo implode("-",$test);Copy after loginResult: hello-world-phpCopy after loginCopy after loginWhat if it is an array in the form of K-V? $test = array("h"=>"hello","w"=>"world","p"=>"php"); echo implode("-",$test);Copy after loginThe result is still hello-world-phpCopy after loginCopy after login, indicating that it still only affects value. 2. Split the string into an array2.1 Split by a certain characterfunction explode ($delimiter, $string, $limit = null) {}Copy after loginexplode (explode, maybe explode the string?) Parameter explanation: delimiter, delimiter limit Document google translation results: If limit is set to positive, the returned array will Contains the maximum limiting element, with the last element containing the remainder of the string. Personal understanding: limit limits the number of split parts. The last part is the remaining string after splitting. Of course, if it is 1, it is the string itself (has passed Experiment) Code demonstration: $str="hello-world-php"; $result = explode("-", $str); var_dump($result); $result = explode("-", $str,2); var_dump($result);Copy after loginOutput result: array(3) { [0]=> string(5) "hello" [1]=> string(5) "world" [2]=> string(3) "php" } array(2) { [0]=> string(5) "hello" [1]=> string(9) "world-php" }Copy after login2.2 Reading by distanceMaybe the string does not need to be divided, but each one needs to be Take out the characters, that is, read them out one by one Original document: ** * Convert a string to an array * @link http://php.net/manual/en/function.str-split.php * @param string $string* The input string. * * @param int $split_length [optional]* Maximum length of the chunk. * * @return array If the optional split_length parameter is * specified, the returned array will be broken down into chunks with each * being split_length in length, otherwise each chunk * will be one character in length. * ** false is returned if split_length is less than 1. * If the split_length length exceeds the length of * string, the entire string is returned as the first * (and only) array element. * @since 5.0 */ function str_split ($string, $split_length = 1) {} Copy after loginPartial translation: Array If the optional split_length parameter is specified, the returned array will is split into chunks of length split_length, otherwise each chunk would be one character long. Convert a string to an array, isn’t this just converting a string into an array, there is nothing to say about the usage Now I want to give it a try, if it is 5 characters in length The string is read in two digits. If there are less than two digits, should the last digit be retained? Of course, presumably for the sake of data integrity, it should be retained. $str = "hello"; var_dump(str_split($str,2));Copy after loginThe result is just as I expected array(3) { [0]=> string(2) "he" [1]=> string(2) "ll" [2]=> string(1) "o" }Copy after loginRecommended learning: php video tutorial The above is the detailed content of How to convert arrays and strings in PHP (must read). For more information, please follow other related articles on the PHP Chinese website! |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |